package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketIssue;
import com.cskaoyan.market.service.MarketIssueService;
import com.cskaoyan.market.service.impl.MarketIssueServiceImpl;
import com.cskaoyan.market.util.HttpUtil2;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/issue/*")
public class MarketIssueController extends HttpServlet {

    // 父接口接收
    private MarketIssueService marketIssueService = new MarketIssueServiceImpl();

    // 因为页面请求方法是get
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/issue/", "");
        if ("list".equals(op)) {
            list(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/issue/", "");
        // 因为create的请求方法是post
        if ("create".equals(op)) {
            create(req, resp);
        } else if ("delete".equals(op)) {
            delete(req, resp);
        } else if ("update".equals(op)) {
            update(req, resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil2.getRequestBody(req);

        // String id = JacksonUtil.parseString(requestBody, "id");
        Integer id = JacksonUtil.parseInteger(requestBody, "id");
        String question = JacksonUtil.parseString(requestBody, "question");
        String answer = JacksonUtil.parseString(requestBody, "answer");
        String addTime = JacksonUtil.parseString(requestBody, "addTime");
        String updateTime = JacksonUtil.parseString(requestBody, "updateTime");
        Boolean deleted = JacksonUtil.parseBoolean(requestBody, "deleted");

        // 判空
        MarketIssue marketIssue = marketIssueService.updateIssue(id, question, answer, addTime, updateTime, deleted);

        Object ok = ResponseUtil.ok(marketIssue);
        resp.getWriter().println(JacksonUtil.writeValueAsString(ok));
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil2.getRequestBody(req);
        String id = JacksonUtil.parseString(requestBody, "id");
        String answer = JacksonUtil.parseString(requestBody, "answer");
        String addTime = JacksonUtil.parseString(requestBody, "addTime");
        String updateTime = JacksonUtil.parseString(requestBody, "updateTime");
        String deleted = JacksonUtil.parseString(requestBody, "deleted");

        int returnCode = marketIssueService.deleteIssue(id);

        // {"errno":0,"errmsg":"成功"}
        if (returnCode == 0) {
            Object ok = ResponseUtil.ok();
            resp.getWriter().println(JacksonUtil.writeValueAsString(ok));
        } else {
            Object fail = ResponseUtil.fail();
            resp.getWriter().println(JacksonUtil.writeValueAsString(fail));
        }

    }


    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 获取请求体
        String requestBody = HttpUtil2.getRequestBody(req);
        String question = JacksonUtil.parseString(requestBody, "question");
        String answer = JacksonUtil.parseString(requestBody, "answer");

        // 判空
        if (StringUtils.isEmpty(question)) {
            Object o = ResponseUtil.fail(605, "问题不能为空");
            String s = JacksonUtil.writeValueAsString(o);
            resp.getWriter().println(s);
            return;
        } else if (StringUtils.isEmpty(answer)) {
            Object o = ResponseUtil.fail(605, "回复不能为空");
            String s = JacksonUtil.writeValueAsString(o);
            resp.getWriter().println(s);
            return;
        }

        // 新增问题
        MarketIssue marketIssue = marketIssueService.createIssue(question, answer);

        Object o = ResponseUtil.ok(marketIssue);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String question = req.getParameter("question");

        List<MarketIssue> marketIssueList = marketIssueService.list(page, limit, sort, order, question);

        Object o = ResponseUtil.okList(marketIssueList);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }
}
